﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.ManagementConsole;
using SharpShare.Configuration;
using System.ComponentModel;

namespace SharpShare.Management.Mmc.Views {
    public class SharesListView : TypeDescriptorReportListView<ShareElement> {
        public SharesListView() {

        }
        public SharesListView(ServiceElement element)
            : this() {
            this.Element = element;
        }

        public ServiceElement Element { get; private set; }

        private ShareElement AddingShareElement { get; set; }

        protected override IEnumerable<ShareElement> ListReportObjects() {
            return this.Element.Shares;
        }

        protected override void PopulateResultNode(Node node, ShareElement reportObject) {
            base.PopulateResultNode(node, reportObject);

            node.ImageIndex = (int)Icon.Hand;
        }
        protected override object GetDescriptionObject() {
            Type type = this.Element.Shares.CreateElement().GetType();
            return new ConfigurationElementTypeDescriptor(TypeDescriptor.GetProvider(type).GetTypeDescriptor(type));
        }
        private void UpdateActions() {
            this.ActionsPaneItems.Clear();

            this.ActionsPaneItems.Add(
                new Microsoft.ManagementConsole.Action("Add...", "Add a new share.", (int)Icon.Add, "Add"));
        }

        protected override void OnAddPropertyPages(PropertyPageCollection propertyPageCollection) {
            PropertyGridPropertyPage page = new PropertyGridPropertyPage(
                new ConfigurationElementTypeDescriptor(TypeDescriptor.GetProvider(this.SelectionData.SelectionObject).GetTypeDescriptor(this.SelectionData.SelectionObject))) {
                    Title = "Share Properties"
                };

            page.Applied += new EventHandler(PropertyPageApplied);
            page.Cancelled += new EventHandler(PropertyPageCancelled);
            propertyPageCollection.Add(page);
        }

        private void PropertyPageCancelled(object sender, EventArgs e) {
            if (this.AddingShareElement != null) {
                this.Element.Shares.Remove(this.AddingShareElement);
                this.AddingShareElement = null;
            }

            this.Refresh();
        }

        private void PropertyPageApplied(object sender, EventArgs e) {
            this.AddingShareElement = null;
            this.Refresh();
        }

        protected override void OnInitialize(AsyncStatus status) {
            if (this.Element == null) {
                this.Element = (this.ViewDescriptionTag as ServiceElement);
            }

            this.SelectionData.EnabledStandardVerbs = (StandardVerbs.Delete | StandardVerbs.Properties);
            this.UpdateActions();

            base.OnInitialize(status);
        }
        protected override void OnAction(Microsoft.ManagementConsole.Action action, AsyncStatus status) {
            base.OnAction(action, status);

            switch ((string)action.Tag) {
                case "Add": {
                        ShareElement newShare = this.Element.Shares.CreateElement();
                        newShare.Name = "(untitled share)";

                        this.Element.Shares.Add(newShare);

                        this.Refresh();

                        this.AddingShareElement = newShare;
                        this.ResultNodes.Cast<ResultNode>().Where(n => n.Tag == newShare).First().SendSelectionRequest(true);

                        break;
                    }
            }
        }
        protected override void OnSelectionChanged(SyncStatus status) {
            base.OnSelectionChanged(status);

            this.UpdateActions();

            if (this.AddingShareElement != null && this.AddingShareElement == this.SelectionData.SelectionObject) {
                this.SelectionData.ShowPropertySheet("Share Properties");
            }
        }

        protected override void OnDelete(SyncStatus status) {
            base.OnDelete(status);

            ShareElement element = (ShareElement)this.SelectionData.SelectionObject;
            this.Element.Shares.Remove(element);

            this.Refresh();
        }
    }
}
